package com.suncaper.tddmall.controller;

import com.suncaper.tddmall.common.model.Cart;
import com.suncaper.tddmall.common.model.CartKey;
import com.suncaper.tddmall.common.model.Goods;
import com.suncaper.tddmall.common.model.User;
import com.suncaper.tddmall.service.CartService;
import com.suncaper.tddmall.service.GoodsService;
import com.suncaper.tddmall.service.UserService;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
public class CartController {
    @Autowired
    private CartService cartService;

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/addWishlist")

    public String toAdd(String goodsId, HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String a = "index";
        if (user == null) {
            a = "register";
        } else {
            Goods goods = goodsService.selectByPrimaryKey(goodsId);
            Cart cart = new Cart();

            cart.setGoodsId(goodsId);
            cart.setUserId(user.getUserId());
//        判断商品是否已经加入购物车
            CartKey cartKey = new CartKey(cart.getUserId(), cart.getGoodsId());
            Cart cart1 = cartService.selectByPrimaryKey(cartKey);
            if (cart1 == null) {
                cart.setGoodsNum(1);
                cart.setTtMoney(goods.getPrice());
                cartService.addWishlist(cart);
            } else {
                cartService.increase(cart1);
            }
        }
        List<Cart> cartList = cartService.selectAll(user.getUserId());
        model.addAttribute("cartList", cartList);
        List<Goods> goodsList = goodsService.selectAll();
        model.addAttribute("goodsList", goodsList);
        return a;
    }

    @RequestMapping("/delete")
    public String delete(String goodsIds, HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String userId = user.getUserId();
        String[] goodsId = goodsIds.split("&");
        cartService.deleteSelect(goodsId, userId);
//        更新cartList
        List<Cart> carts = cartService.selectAll(userId);
        model.addAttribute("cartLists", carts);
        return "../static/page/cart";
    }

    @RequestMapping("/showCart")
    public String showCart(String userId, HttpServletRequest request, Model model) {
        String a = "pleaseLogin";
        BigDecimal bigDecimal = new BigDecimal(0);
        if (userId.equals(a)) {
            return "index";
        } else {
            List<Cart> carts = cartService.selectAll(userId);

            for (Cart cart : carts) {
                Goods goods = goodsService.selectByPrimaryKey(cart.getGoodsId());
                cart.setGoods(goods);
                bigDecimal = bigDecimal.add(goods.getPrice());
            }
            model.addAttribute("cartList", carts);
            model.addAttribute("ttMoney", bigDecimal);
            return "../static/page/cart";
        }

    }

    @RequestMapping("/increase")
    @ResponseBody
    public String increase(String goodsId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
//        user.setUserId("admin");
        CartKey cartKey = new CartKey(user.getUserId(), goodsId);
        Cart cart = cartService.selectByPrimaryKey(cartKey);
        cartService.increase(cart);
        return "修改成功";
    }

    @RequestMapping("/decrease")
    @ResponseBody
    public String decrease(String goodsId, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
/*        User user=new User();
        user.setUserId("admin");*/
        CartKey cartKey = new CartKey(user.getUserId(), goodsId);
        Cart cart = cartService.selectByPrimaryKey(cartKey);
        cartService.decrease(cart);
        return "修改成功";
    }
}
